/*
 * Copyright (C) 2011 The Code Bakers
 * Authors: Cleuton Sampaio e Francisco Rodrigues
 * e-mail: thecodebakers@gmail.com
 * Project: http://code.google.com/p/biomagohc/
 * Site: http://www.thecodebakers.org
 *
 * Licensed under the GNU GPL, Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://gplv3.fsf.org/
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * @author Cleuton Sampaio e Francisco Rogrigues - thecodebakers@gmail.com
 */

package br.com.thecodebakers.biomago.activities;

import java.util.Calendar;
import java.util.Date;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import br.com.thecodebakers.biomago.R;
import br.com.thecodebakers.biomago.business.BioBO;
import br.com.thecodebakers.biomago.model.Elemento;

public class Edicao extends Activity {
	private BioBO bo;
	private static final String TAG = "Edição";
	private EditText txtNome;
	private DatePicker dtNascimento;
	private Edicao selfRef;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.edicao);
        bo = BioBO.getInstance(this.getApplicationContext());
        selfRef = this;
	}
	
	public void salvar (View view) {
		txtNome = (EditText) this.findViewById(R.id.txtNome);
		dtNascimento = (DatePicker) this.findViewById(R.id.dtData);
		
		// Há um bug com o DatePicker. Se você editar o ano, ele perde o valor
		// O "Image" não pode receber o foco, logo, temos que forçar a retirada 
		// do foco do DatePicker
		txtNome.requestFocus();
		dtNascimento.clearFocus();
		// Fim macete
		
		Calendar cal = Calendar.getInstance();
		Date hoje = new Date();
		Elemento elemento = new Elemento();
		elemento.setNome(txtNome.getText().toString());
		cal.set(dtNascimento.getYear(), 
				dtNascimento.getMonth(), 
				dtNascimento.getDayOfMonth());
		elemento.setNascimento(cal.getTime());
		
		if (cal.getTime().getTime() > hoje.getTime()) {
			Resources res = this.getResources();
	        new AlertDialog.Builder(this).setMessage(res.getString(R.string.erro_data_posterior))
	        .setNeutralButton(res.getString(R.string.lblOK), new DialogInterface.OnClickListener() {
	            public void onClick(DialogInterface dialog, int which) {
	            	selfRef.finish();
	            } }).show(); 
		}
		else {
			if (bo.getListaPessoas().contains(elemento)) {
				Resources res = this.getResources();
		        new AlertDialog.Builder(this).setMessage(res.getString(R.string.msg_inclusao_duplicidade))
		        .setNeutralButton(res.getString(R.string.lblOK), new DialogInterface.OnClickListener() {
		            public void onClick(DialogInterface dialog, int which) {
		            	selfRef.finish();
		            } }).show(); 
		        
			}
			else {
				int retorno = bo.AddPessoa(elemento);
				
				Resources res = this.getResources();
		        new AlertDialog.Builder(this).setMessage(res.getString(retorno))
		        .setNeutralButton(res.getString(R.string.lblOK), new DialogInterface.OnClickListener() {
		            public void onClick(DialogInterface dialog, int which) {
		            } }).show(); 
		        if (retorno == R.string.msg_inclusao_ok) {
		        	txtNome.setText("");
		        	Calendar cal2 = Calendar.getInstance();
		        	dtNascimento.updateDate(cal2.get(Calendar.YEAR), cal2.get(Calendar.MONTH), cal2.get(Calendar.DAY_OF_MONTH));
		        }
			}
		}
	}
	
	public void cancelar (View view) {
		this.finish();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.menu_contexto_inclusao, menu);
	    return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.mnuAjuda:
			Intent i = new Intent (this.getApplicationContext(), Ajuda.class);
			this.startActivity(i);
			return true;
		default: return super.onOptionsItemSelected(item); 
		}
	}
}
